package com.gg.user.gguser.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gg.encryption.AES;
import com.gg.header.getHeaderUser;
import com.gg.user.gguser.dao.GgBindingDaoMapper;
import com.gg.user.gguser.dao.GgUserDao;
import com.gg.user.gguser.dao.GgUserDaoMapper;
import com.gg.user.gguser.dataobject.GgUserDataobject;
import com.gg.user.gguser.service.StaffService;
import com.gg.vo.return_format;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author yangxinlei
 * @date 2020/4/19
 */
@Service
public class StaffServiceImpl implements StaffService {

    @Autowired
    private GgUserDaoMapper ggUserDaoMapper;

    @Autowired
    private GgUserDao ggUserDao;

    @Autowired
    private GgBindingDaoMapper ggBindingDaoMapper;
    /**
     * 获取用户列表
     *
     * role 用户类型
     *
     * page 页面页数
     * pageNumer 每页个数
     *
     */
    @Override
    public Object getUserStaff(HttpServletResponse response, HttpServletRequest request, Integer page, Integer pageNumber) throws Exception {
        try {
            // 查看是不是内部用户，是内部用户的话能看到全部 不是的话只能看到自己下级的
            // 获取用户信息
            // 从cookice信息中过去用户信息 在这里只用要id就好
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            Integer ggGrade= JSONObject.parseObject(cookiceUser).getInteger("ggGrade");
            String getUserId = JSONObject.parseObject(cookiceUser).getString("ggId");
            if(ggGrade==1){
                Integer getUserSelectCount = ggUserDaoMapper.getRootUserSelectCountGrade(null);
                List<GgUserDataobject> userList = ggUserDaoMapper.getRootUserSelectCount((page - 1) * pageNumber, pageNumber,null);
                return return_format.return_format_All(0,return_format.return_format_data_map(page, (int) Math.ceil(getUserSelectCount/pageNumber),pageNumber,getUserSelectCount,userList),"");
            }else if(ggGrade==2){
                // 查询出全部客户
                Integer getUserSelectCount = ggUserDaoMapper.getRootUserSelectCountGrade(3);
                List<GgUserDataobject> userList = ggUserDaoMapper.getRootUserSelectCount((page - 1) * pageNumber, pageNumber,3);
                return return_format.return_format_All(0,return_format.return_format_data_map(page, (int) Math.ceil(getUserSelectCount/pageNumber),pageNumber,getUserSelectCount,userList),"");
            }else if(ggGrade==3){
                // 公司身份查询出自己下面的
                Integer getUserSelectCount = ggUserDaoMapper.getGgBinDing(getUserId);
                List<GgUserDataobject> userList = ggUserDaoMapper.getGgBinDingSelectCount((page - 1) * pageNumber, pageNumber,getUserId);
                return return_format.return_format_All(0,return_format.return_format_data_map(page, (int) Math.ceil(getUserSelectCount/pageNumber),pageNumber,getUserSelectCount,userList),"");
            }else if(ggGrade==4 || ggGrade==5){
                //身份有点太低没有模块权限
                return return_format.return_format_error(10002,"您没有此模块权限！");
            }else {
                // 吓传数据
                return return_format.return_format_error(10002,"请登陆！");
            }
        }catch (Exception e){
            return return_format.return_format_error(10002,"服务器出问题！");
        }
    }

    /**
     * 状态修改
     * @param response
     * @param request
     * @param status  现在状态
     * @return
     * @throws Exception
     */
    @Override
    public Object setStatus(HttpServletResponse response, HttpServletRequest request, Integer status,String userId) throws Exception {
        try {
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            Integer ggGrade= JSONObject.parseObject(cookiceUser).getInteger("ggGrade");
            String getUserId = JSONObject.parseObject(cookiceUser).getString("ggId");
            if(ggGrade == 1){
                // 超级管理员谁都可以改
                return staticUsersetStatus(status,userId);
            }else if(ggGrade == 2){
                Integer getGgGrade = ggUserDao.getUser(userId).getGgGrade();//获取要修改用户的等级
                if(getGgGrade > 2 && getGgGrade < 6){
                    return staticUsersetStatus(status,userId);
                }else {
                    return return_format.return_format_error(10003,"等级不足不要瞎改!");
                }
            }else if(ggGrade == 3){
                //修改自己公司下面的
                Integer relationship = ggBindingDaoMapper.getCountBinding(getUserId,userId);//查看有绑定关系没
                if(relationship == 1){
                    return staticUsersetStatus(status,userId);
                }else {
                    return return_format.return_format_error(10003,"等级不足不要瞎改!");
                }
            }else {
                return return_format.return_format_error(10003,"等级不足!");
            }
        }catch (Exception e){
            return return_format.return_format_error(10003,"服务器报错！!");
        }

    }

    /**
     * 修改密码
     * @param response
     * @param request
     * @param userId  用户id
     * @param userPassWord  修改的密码
     * @return
     * @throws Exception
     */
    @Override
    public Object updatePassWord(HttpServletResponse response, HttpServletRequest request, String userId, String userPassWord) throws Exception {
        try {
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            Integer ggGrade= JSONObject.parseObject(cookiceUser).getInteger("ggGrade");
            String getUserId = JSONObject.parseObject(cookiceUser).getString("ggId");
            if(ggGrade == 1){
                //超管都可以改
                GgUserDataobject ggUserDataobject = ggUserDao.getUser(userId);//获取要修改用户的等级
                ggUserDaoMapper.updatePassWord(userId,AES.AESEncode(ggUserDataobject.getToken(),userPassWord));
                return return_format.return_format_All(0,"","");
            }else if(ggGrade == 2){
                //客服只能改外部的
                GgUserDataobject ggUserDataobject = ggUserDao.getUser(userId);//获取要修改用户的等级
                if(ggUserDataobject.getGgGrade() > 2 && ggUserDataobject.getGgGrade() < 6){
                    ggUserDaoMapper.updatePassWord(userId,AES.AESEncode(ggUserDataobject.getToken(),userPassWord));
                    return return_format.return_format_All(0,"","");
                }else {
                    return return_format.return_format_error(10003,"等级不足不要瞎改!");
                }
            }else if(ggGrade == 3){
                //修改自己公司下面的
                Integer relationship = ggBindingDaoMapper.getCountBinding(getUserId,userId);//查看有绑定关系没
                GgUserDataobject ggUserDataobject = ggUserDao.getUser(userId);//获取要修改用户的等级
                if(relationship == 1){
                    ggUserDaoMapper.updatePassWord(userId,AES.AESEncode(ggUserDataobject.getToken(),userPassWord));
                    return return_format.return_format_All(0,"","");
                }else {
                    return return_format.return_format_error(10003,"等级不足不要瞎改!");
                }
            }else {
                return return_format.return_format_error(10003,"等级不足!");
            }
        }catch (Exception e){
            return return_format.return_format_error(10003,"服务器报错！!");
        }


    }

    /**
     * 用户个人中心修改东西
     * @param response
     * @param request
     * @param type      修改类型
     * @param content   修改内容
     * @return
     * @throws Exception
     */
    @Override
    public Object userUpdatePersonal(HttpServletResponse response, HttpServletRequest request, Integer type, String content) throws Exception {
        try {
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            String getUserId = JSONObject.parseObject(cookiceUser).getString("ggId");
            if(type == 1){
                // 修改昵称
                ggUserDaoMapper.updatNiCheng(getUserId,content);
                return return_format.return_format_All(0,"","");
            }else if(type == 2){
                // 修改密码
                GgUserDataobject ggUserDataobject = ggUserDao.getUser(getUserId);//获取要修改用户的信息
                ggUserDaoMapper.updatePassWord(getUserId,AES.AESEncode(ggUserDataobject.getToken(),content));
                return return_format.return_format_All(0,"","");
            }else if(type == 3){
                // 修改绑定手机号
                ggUserDaoMapper.updatPhone(getUserId,content);
                return return_format.return_format_All(0,"","");
            }else if(type == 4){
                // 修改邮箱
                ggUserDaoMapper.updatYouXiang(getUserId,content);
                return return_format.return_format_All(0,"","");
            }else {
                // 类型错误
                return return_format.return_format_error(10003,"等级不足不要瞎改!");
            }
        }catch (Exception e){
            return return_format.return_format_error(10003,"服务器报错！");
        }
    }


    // 定义本地方法抽取   修改用户状态
    public Object staticUsersetStatus(Integer status,String userid)throws Exception{
        if(status == 1){
            //修改启用
            try {
                ggUserDaoMapper.setStatus(0,userid);
                return return_format.return_format_All(0,"","");
            }catch (Exception e){
                return return_format.return_format_error(10002,"服务器报错！！");
            }

        }else if (status == 0){
            // 修改禁用
            try {
                ggUserDaoMapper.setStatus(1,userid);
                return return_format.return_format_All(0,"","");
            }catch (Exception e){
                return return_format.return_format_error(10002,"服务器报错！！");
            }
        }else {
            // 瞎弄的
            return return_format.return_format_error(10003,"不要瞎改!");
        }
    }
}
